﻿package nasif.cn.imagewikis.common.help;



import java.util.ArrayList;
import java.util.List;

/**
 * 分页辅助类，调用getPages()获得当前分页数的列表（如第3页、第4页...），并尽量是curPage当前页位于正中位置
 * @author moyan.wwl
 *
 */
public class PagingHelper {
	
	// 默认分页栏显示的分页数目
	private static final int DEFAULT_PAGE_RANGE = 5;
	
	// 默认每页记录数目
	private static final int DEFAULT_PAGE_SIZE = 20;
	
	// 分页栏显示的分页数目
	private int pageRange;
	
	// 当前页
	private int curPage;

	// 起始页
	private int startPage;

	// 结束页
	private int endPage;

	// 总页数
	private int totalPage;
	
	// 每页显示记录数目
	private int pageSize;
	
	// 记录总数
	private long totalItem;
	
	private List<Integer> pages = new ArrayList<Integer>();
	
	public PagingHelper(Integer curPage, Long totalItem, Integer pageSize, Integer pageRange) 
			throws NullPointerException, IllegalArgumentException{
		if(pageSize == null || pageRange == null) {
			throw new NullPointerException("分页构造参数和pageSize和pageRange不可为空！");
		}
		
		if(pageSize < 1 || pageRange < 1) {
			throw new IllegalArgumentException("分页构造参数pageSize和pageRange不可小于1！");
		}

		if(totalItem <= 0) {
			return;
		}
		
		if(curPage == null) {
			curPage = 1;
		}
		
		this.curPage = curPage;
		this.totalItem = totalItem;
		this.pageSize = pageSize;
		this.pageRange = pageRange;
		
		countPage();
	}

	public PagingHelper(Integer curPage, Long totalItem, Integer pageSize) {
		this(curPage, totalItem, pageSize, DEFAULT_PAGE_RANGE);
	}
	
	public PagingHelper(Integer curPage, Long totalItem) throws NullPointerException, IllegalArgumentException {
		this(curPage, totalItem, DEFAULT_PAGE_SIZE, DEFAULT_PAGE_RANGE);
	}
	
	/**
	 * 计算分页的各项参数
	 */
	private void countPage() {
		if(totalItem <= 0) {
			return;
		}
		
		totalPage = (int) totalItem / pageSize;
		if(totalItem % pageSize > 0) {
			totalPage++;
		}
	
		// 调校页号
		if(curPage > totalPage) {
			curPage = totalPage;
		} else if(curPage < 1) {
			curPage = 1;
		}

		// 使当前页排在显示页的中间
		startPage = curPage - pageRange / 2;
		if (startPage < 1) {
			startPage = 1;
		}

		// 调校结束页
		endPage = pageRange + startPage - 1;
		if (endPage > totalPage) {
			endPage = totalPage;
		}

		// 调校起始页
		startPage = endPage - pageRange + 1;
		if (startPage < 1) {
			startPage = 1;
		}

		for (int i = startPage; i <= endPage; i++) {
			pages.add(i);
		}
	}

	public int getPageRange() {
		return pageRange;
	}

	public int getCurPage() {
		return curPage;
	}

	public int getStartPage() {
		return startPage;
	}

	public int getEndPage() {
		return endPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public long getTotalItem() {
		return totalItem;
	}
	
	public List<Integer> getPages() {
		return pages;
	}
	
	public int getNextPage() {
		return curPage + 1 > totalPage? totalPage: curPage + 1;
	}
	
	public int getPrevPage() {
		return curPage - 1 < 1? 1: curPage - 1;
	}
	
	public int getFirstPage() {
		return pages.size() > 0? pages.get(0) : 0;
	}
	
	public int getLastPage() {
		return pages.size() > 0? pages.get(pages.size() - 1) : 0;
	}
	
	public int getStartRow() {
		return (curPage - 1) * pageSize;
	}
}
